package com.blockchain.nodes.web.controller.manager;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.blockchain.nodes.common.annotation.Log;
import com.blockchain.nodes.common.enums.BusinessType;
import com.blockchain.nodes.manager.domain.TransactionFlow;
import com.blockchain.nodes.manager.service.ITransactionFlowService;
import com.blockchain.nodes.framework.web.base.BaseController;
import com.blockchain.nodes.common.page.TableDataInfo;
import com.blockchain.nodes.common.base.AjaxResult;
import com.blockchain.nodes.common.utils.poi.ExcelUtil;

/**
 * 交易流水 信息操作处理
 * 
 * @author Gene Max
 * @date 2019-03-08
 */
@Controller
@RequestMapping("/manager/transactionFlow")
public class TransactionFlowController extends BaseController
{
    private String prefix = "manager/transactionFlow";
	
	@Autowired
	private ITransactionFlowService transactionFlowService;
	
	@RequiresPermissions("manager:transactionFlow:view")
	@GetMapping()
	public String transactionFlow()
	{
	    return prefix + "/transactionFlow";
	}
	
	/**
	 * 查询交易流水列表
	 */
	@RequiresPermissions("manager:transactionFlow:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TransactionFlow transactionFlow)
	{
		startPage();
        List<TransactionFlow> list = transactionFlowService.selectTransactionFlowList(transactionFlow);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出交易流水列表
	 */
	@RequiresPermissions("manager:transactionFlow:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TransactionFlow transactionFlow)
    {
    	List<TransactionFlow> list = transactionFlowService.selectTransactionFlowList(transactionFlow);
        ExcelUtil<TransactionFlow> util = new ExcelUtil<TransactionFlow>(TransactionFlow.class);
        return util.exportExcel(list, "transactionFlow");
    }
	
	/**
	 * 新增交易流水
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存交易流水
	 */
	@RequiresPermissions("manager:transactionFlow:add")
	@Log(title = "交易流水", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TransactionFlow transactionFlow)
	{		
		return toAjax(transactionFlowService.insertTransactionFlow(transactionFlow));
	}

	/**
	 * 修改交易流水
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		TransactionFlow transactionFlow = transactionFlowService.selectTransactionFlowById(id);
		BigDecimal walletBalance = new BigDecimal(transactionFlow.getAmount(), new MathContext(8)).divide( new BigDecimal(100000000));
    	java.text.DecimalFormat df = new java.text.DecimalFormat("#0.########");
    	transactionFlow.settAmount(df.format(walletBalance));
		mmap.put("transactionFlow", transactionFlow);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存交易流水
	 */
	@RequiresPermissions("manager:transactionFlow:edit")
	@Log(title = "交易流水", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TransactionFlow transactionFlow)
	{		
		return toAjax(transactionFlowService.updateTransactionFlow(transactionFlow));
	}
	
	/**
	 * 删除交易流水
	 */
	@RequiresPermissions("manager:transactionFlow:remove")
	@Log(title = "交易流水", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(transactionFlowService.deleteTransactionFlowByIds(ids));
	}
	
}
